package com.ynr.voucher.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ynr.voucher.dao.IVoucherDao;
import com.ynr.voucher.service.IVoucherSalaryService;
import com.ynr.voucher.service.IVoucherService;

@Service
public class VoucherSalaryServiceImpl implements IVoucherSalaryService {

	private static final Logger logger = LoggerFactory.getLogger(VoucherSalaryServiceImpl.class);
	
	@Autowired
	private IVoucherService voucherService;
	
	@Autowired
	private IVoucherDao voucherDao;
	
	@Override
	public void processSalary(String companyId, String billDate, List<Map<String, Object>> salaryContent) {
		Map<String, Integer> retMap = voucherDao.getLastVoucherNum(billDate, "sales");
		int lastVoucherNum = 0;
		if(retMap != null && !retMap.isEmpty()) {
			lastVoucherNum = retMap.get("lastVoucherNum");
		} else {
			retMap = voucherDao.getLastVoucherNum(billDate, "invoice");
			if(retMap != null && !retMap.isEmpty()) {
				lastVoucherNum = retMap.get("lastVoucherNum");
			}
		}
		int voucherNum = lastVoucherNum+1;
		logger.debug("voucherNum : {}", voucherNum);
		voucherService.generateSalaryVoucher(companyId, billDate, salaryContent, voucherNum);
	}

}
